package com.dx.cms.Test;

import java.sql.*;

/**
 * @Auther: zephyrus
 * @Date: 2023/11/23
 * @Description:
 * @since
 */
public class TestUpdate {
    public static void main(String[] args) throws SQLException {
        //利用jdbc-driver驱动操作数据库
        //1. 加载驱动程序
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("找不到驱动");
        }
        Connection conn=null;
        //2. 创建连接
        try {
            conn= DriverManager
                    .getConnection("jdbc:mysql://localhost:3306/jsp","root","root");
        } catch (SQLException e) {
            System.out.println("创建连接失败");
        }
        //3 创建语句对象
        //Statement st=conn.createStatement();
        PreparedStatement ps=conn.prepareStatement("update tb_user set username=?,password=?,real_name=? where id=?");
                ps.setString(1,"aaa");
        ps.setString(2,"aaa");
        ps.setString(3,"aaa");
        ps.setInt(4,3);
        //执行语句 insert update delete
        int count=ps.executeUpdate();
                //st.executeUpdate("update tb_user set username='lisi1314' where id=3");


        if(count==1){
            System.out.println("数据更新成功");
        }else{
            System.out.println("数据更新失败");
        }

        ps.close();
        conn.close();
    }
}
